﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using System.Collections;
namespace ServiceOTOForm
{
    public partial class THONGTINXE : DevExpress.XtraEditors.XtraForm
    {
        public THONGTINXE()
        {
            InitializeComponent();
        }
        private int TT = 0;
        private OTOSW.DataIn dti = new OTOSW.DataIn();
       private OTOSW.HANGXE_ENTITY hxEntity = new OTOSW.HANGXE_ENTITY();
       private OTOSW.XE_ENTITY xeEntity = new OTOSW.XE_ENTITY();
        private void THONGTINXE_Load(object sender, EventArgs e)
        {
            LoadDanhSachHangXe();
            string MAH = grdHangXeDetail.GetFocusedRowCellValue("MAHANG").ToString();
            GetXeByMAH(MAH);
            Get_MAHANGONLY();
            txtHinhAnh.Properties.ReadOnly=true;
            grdMain.CloseEditor();
         }

        private void Get_MAHANGONLY()
        {
            cbMAHANG.Properties.DataSource = dti.cbMAHANG(hxEntity).Tables[0];
           
            cbMAHANG.Properties.DisplayMember = "TENHANG";
            cbMAHANG.Properties.ValueMember = "MAHANG";
        }

        private void LoadDanhSachHangXe()
        {

            try
            {

                DataSet ds = new DataSet();
                ds = dti.DanhSachHangXe(hxEntity);
                grdHangXe.DataSource = ds.Tables[0];



            }
            catch
            {

            }
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
           LuuThongTinXE();
            THONGTINXE_Load(sender, e);

        }

        
        private void btHinhanh_Click(object sender, EventArgs e)
        {
            //luu hinh anh
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "jpeg|*.jpg|png|*.png|gif|*.gif";
            if (op.ShowDialog() == DialogResult.OK)
            {
                txtHinhAnh.Text = op.SafeFileName;
            }
        }
        private void GetXeByMAH(string MaH)
        {
            try
            {

                xeEntity.MAHANG = MaH;
                DataSet ds = new DataSet();
                ds = dti.XEBYHANG(xeEntity);
               
                if (ds.Tables[0].Rows.Count > 0)
                {
                    gridControl2.DataSource = ds.Tables[0];
                }
                else
                {
                    Clear();
                    gridControl2.DataSource = null;
                }
           }
            catch(Exception ex)
            {
                //throw new Exception(ex.ToString());
            }

        }

    

        private void grdHangXeDetail_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

            try
            {
                string MAH = grdHangXeDetail.GetFocusedRowCellValue("MAHANG").ToString();
                GetXeByMAH(MAH);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

        }

        private void GetValue_GridMain()
        {
            try
            {
                xeEntity.MAXE = grdMain.GetFocusedRowCellValue("MAXE").ToString();
                DataSet ds = dti.TTXETHEOMA(xeEntity);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        txtMaXe.Text = dr["MAXE"].ToString();

                        cbMAHANG.EditValue = dr["MAHANG"].ToString();
                        txtTenXe.Text = dr["TENXE"].ToString();
                        cbQuocGia.Text = dr["XUATXU"].ToString();
                        cbMauSac.Text = dr["MAUSAC"].ToString();
                        txtNhienLieu.Text =dr["NHIENLIEU"].ToString();
                        cbSoCho.Text =dr["SOCHO"].ToString();
                        cbSoCua.Text = dr["SOCUA"].ToString();
                        txtHopSo.Text = dr["HOPSO"].ToString();
                        txtPhanh.Text =dr["HETHONGPHANH"].ToString();
                        txtDongCo.Text = dr["DONGCO"].ToString();
                        txtDungTichnl.Text = dr["DUNGTICHBINHNHIENLIEU"].ToString();
                        txtDTXiLanh.Text = dr["DUNGTICHXILANH"].ToString();
                        txtTrongLuong.Text = dr["TRONGLUONG"].ToString();
                        string TBDV = dr["THIETBIDINHVI"].ToString();
                        if (TBDV == "True")
                        {
                            checkDV.Checked = true;
                        }
                        else
                        {
                            checkDV.Checked = false;
                        }
                        string TAYLAITROLUC = dr["TAYLAITROLUC"].ToString();
                        if (TAYLAITROLUC == "True")
                        {
                            checkTroluc.Checked = true;
                        }
                        else
                        {
                            checkTroluc.Checked = false;
                        }
                        string DIEUHOA = dr["DIEUHOA"].ToString();
                        if (DIEUHOA == "True")
                        {
                            checkDieuhoa.Checked = true;
                        }
                        else
                        {
                            checkDieuhoa.Checked = false;
                        }


                        string THIETBIGIAITRI = dr["THIETBIGIAITRI"].ToString();
                        if (THIETBIGIAITRI == "True")
                        {
                            checkGiaitri.Checked = true;
                        }
                        else
                        {
                            checkGiaitri.Checked = false;
                        }
                        string TUIKHIANTOAN = dr["TUIKHIANTOAN"].ToString();
                        if (TUIKHIANTOAN == "True")
                        {
                            checktuihi.Checked = true;
                        }
                        else
                        {
                            checktuihi.Checked = false;
                        }
                        string HETHONGBAOTROM = dr["HETHONGBAOTROM"].ToString();
                        if (HETHONGBAOTROM == "True")
                        {
                            checkbaotrom.Checked = true;
                        }
                        else
                        {
                            checkbaotrom.Checked = false;
                        }
                        string CHONGBOCUNGPHANH = dr["CHONGBOCUNGPHANH"].ToString();
                        if (CHONGBOCUNGPHANH == "True")
                        {
                            checkBCphanh.Checked = true;
                        }
                        else
                        {
                            checkBCphanh.Checked = false;
                        }
                        string DIEUKHIENHANHTRINH =dr["DIEUKHIENHANHTRINH"].ToString();
                        if (DIEUKHIENHANHTRINH == "True")
                        {
                            checkHT.Checked = true;
                        }
                        else
                        {
                            checkHT.Checked = false;
                        }
                        txtGiaBan.Text = dr["GIABAN"].ToString();
                        txtHinhAnh.Text = dr["HINHANH"].ToString();
                    }
                   
                }
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        private void grdMain_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (TT == 0)
                {

                    GetValue_GridMain();
                }
                 
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
       private void Clear()
        {


            txtMaXe.Text = "";
            txtTenXe.Text = "";           
            cbMAHANG.EditValue = null;
            cbMauSac.EditValue = null;
            cbSoCho.EditValue = null;
            cbSoCua.EditValue = null;
           // cbTenXe.EditValue = null;
            cbQuocGia.EditValue = null;
            txtNhienLieu.Text = "";
            txtHopSo.Text = "";
            txtPhanh.Text = "";
            txtDongCo.Text = "";
            txtNhienLieu.Text = "";
            txtDungTichnl.Text = "";
            txtDTXiLanh.Text = "";
            txtGiaBan.Text = "";
            txtTrongLuong.Text = "";
            checkDV.Checked = false;
            checkTroluc.Checked= false;
            checkDieuhoa.Checked = false;
            checkGiaitri.Checked = false;
            checktuihi.Checked = false;
            checkbaotrom.Checked = false;
            checkBCphanh.Checked = false;
            checkHT.Checked = false;
        }
        private void ActiveButton(bool flag)
        {

            btLuu.Enabled = flag;
            btHuy.Enabled = flag;
            btThem.Enabled = !flag;
            btSua.Enabled = !flag;
            btXoa.Enabled = !flag;

        }
        //kiem tra trung MaXe
        public Hashtable HashListMAXE()
        {

            Hashtable ht = new Hashtable();
            
            DataSet ds = dti.Check_MaXE(xeEntity);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ht.Add(dr["MAXE"], dr["TENXE"]);
                }
            }
            return ht;
        }
        //====
        private void LuuThongTinXE()
        {
            if (TT == 1)
            {
                Hashtable hs = HashListMAXE();
                if (hs.ContainsKey(txtMaXe.Text))
                {
                    MessageBox.Show("Mã Xe đã tồn tại", "Thông Báo", MessageBoxButtons.OK);
                    return;
                }
            }

            // if (KiemTraNhapLieu())
            if (!dxValidationProvider1.Validate()) return;
            {

                xeEntity.MAXE = txtMaXe.Text;
                xeEntity.TENXE = txtTenXe.Text;
                xeEntity.MAHANG = cbMAHANG.GetColumnValue("MAHANG").ToString();
                xeEntity.XUATXU = cbQuocGia.SelectedItem.ToString();
                xeEntity.NHIENLIEU = txtNhienLieu.Text;
                xeEntity.SOCHO = int.Parse(cbSoCho.SelectedItem.ToString());
                xeEntity.SOCUA = int.Parse(cbSoCua.SelectedItem.ToString());
                xeEntity.HOPSO = txtHopSo.Text;
                xeEntity.HETHONGPHANH = txtPhanh.Text;
                xeEntity.DONGCO = txtDongCo.Text;
                xeEntity.DUNGTICHBINHNHIENLIEU = decimal.Parse(txtDungTichnl.Text);
                xeEntity.DUNGTICHXILANH = txtDTXiLanh.Text;
                xeEntity.TRONGLUONG = decimal.Parse(txtTrongLuong.Text);
                xeEntity.GIABAN = decimal.Parse(txtGiaBan.Text);
                xeEntity.MAUSAC = cbMauSac.SelectedItem.ToString();

                if (checkDV.Checked=true)
                {
                    xeEntity.THIETBIDINHVI = true;
                }
                else
                {
                    xeEntity.THIETBIDINHVI = false;
                }
                if (checkTroluc.Checked == true)
                {
                    xeEntity.TAYLAITROLUC = true;
                }

                else
                {
                    xeEntity.TAYLAITROLUC = false;
                }
                if (checkDieuhoa.Checked == true)
                {
                    xeEntity.DIEUHOA = true;
                }

                else
                {
                    xeEntity.DIEUHOA = false;
                }
                if (checkGiaitri.Checked == true)
                {
                    xeEntity.THIETBIGIAITRI = true;
                }

                else
                {
                    xeEntity.THIETBIGIAITRI = false;
                }
                if (checktuihi.Checked == true)
                {
                    xeEntity.TUIKHIANTOAN = true;
                }

                else
                {
                    xeEntity.TUIKHIANTOAN = false;
                }
                if (checkbaotrom.Checked == true)
                {
                    xeEntity.HETHONGBAOTROM = true;
                }

                else
                {
                    xeEntity.HETHONGBAOTROM = false;
                }
                if (checkBCphanh.Checked == true)
                {
                    xeEntity.CHONGBOCUNGPHANH = true;
                }

                else
                {
                    xeEntity.CHONGBOCUNGPHANH = false;
                }
                if (checkHT.Checked == true)
                {
                    xeEntity.DIEUKHIENHANHTRINH = true;
                }

                else
                {
                    xeEntity.DIEUKHIENHANHTRINH = false;
                }
                xeEntity.GIABAN = decimal.Parse(txtGiaBan.Text);
                xeEntity.HINH = txtHinhAnh.Text;
                if (TT == 1)//THEM
                {
                    try
                    {

                        dti.LuuThongTinXE(xeEntity);
                        MessageBox.Show("Thêm thành công Xe "  + txtTenXe.Text,  "Thông báo", MessageBoxButtons.OK);
                        ActiveButton(false);
                      //  LoadDanhSachNCC();

                    }
                    catch
                    {
                        MessageBox.Show("Xảy ra lỗi khi thêm Xe" + txtTenXe.Text, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    try
                    {
                        dti.LuuThongTinXE(xeEntity);
                        MessageBox.Show("Cập nhật thành công   Xe" + txtTenXe.Text, "Thông báo", MessageBoxButtons.OK);
                        ActiveButton(false);
                       // LoadDanhSachNCC();
                    }
                    catch
                    {
                        MessageBox.Show("Xảy ra lỗi khi cập nhật Xe" + txtTenXe.Text, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                Clear();
                TT = 0;
                //GetValue_GridMain(); ;

            }
        }
        private void btThem_Click(object sender, EventArgs e)
        {
            Clear();
            TT = 1;
            ActiveButton(true);
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            TT = 2;
            txtMaXe.Properties.ReadOnly = true;
            ActiveButton(true);
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            try
            {
                Clear();
                GetValue_GridMain();
                ActiveButton(false);
            }
            catch
            {

            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            xeEntity.MAXE = txtMaXe.Text;
            try
            {
                if (MessageBox.Show("Bạn có muốn xoá XE " + txtTenXe.Text + " không ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    dti.XoaThongTinXe(xeEntity);
                    MessageBox.Show("Xoá thành công XE " + txtTenXe.Text, "Thông báo", MessageBoxButtons.OK);
                    Clear();
                  //  LoadDanhSachNCC();
                    //GetValue_GridMain();//============
                    THONGTINXE_Load(sender, e);
                }
            }
            catch
            {

            }
        }

        private void grdMain_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                if (TT == 0)
                {

                    GetValue_GridMain();
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        private void grdHangXeDetail_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                string MAH = grdHangXeDetail.GetFocusedRowCellValue("MAHANG").ToString();
                GetXeByMAH(MAH);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
    }
}